import Vue from 'vue';
import VueRouter from 'vue-router';

const Login = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../view/Login.vue')

const Home = () => import(/* webpackChunkName: "Home_Home_Welcome" */ '../view/Home.vue')

const HomeUser = () => import(/* webpackChunkName: "HomeUser_Home_Welcome" */ '../view/HomeUser.vue')
// 安装路由插件
Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      title: '盈芯科技数据查询后台'
    }
  },
  {
    path: '/homeUser',
    name: 'HomeUser',
    component: HomeUser,
    meta: {
      title: '盈芯科技数据查询后台'
    }
  },
];


// vue3的写法
// const router = createRouter({
//     history: createWebHistory(process.env.BASE_URL),
//     routes
//   });

// 创建路由实例
const router = new VueRouter({
  mode: 'hash', // 路由模式，可选 'hash' 或 'history'
  base: process.env.BASE_URL,
  routes
});


// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch(err => err)
// }


router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next();
});

export default router;